package kr.co.fashionmai.util;

public class FabricPageUtil 
{
	private int pageNo;
	private int totalCount;
	
	private boolean hasPrevious;
	private boolean hasNext;
	
	private int firstPageNo;
	private int lastPageNo;
	
	public FabricPageUtil(int pageNo, int totalCount)
	{
		this.pageNo = pageNo;
		this.totalCount = totalCount;
		makeFirstPageNo();
		makeLastPageNo();
	}
	
	private void makeFirstPageNo()
	{
		this.firstPageNo = (int)Math.ceil(pageNo/(double)10) * 10-9;
		if(firstPageNo!=1) this.hasPrevious = true;
		this.lastPageNo = this.firstPageNo + 9 ;
	}
	
	private void makeLastPageNo()
	{
		int tempLastPageNo = (int) Math.ceil(this.totalCount/(double)20);
		if( tempLastPageNo < this.lastPageNo) this.lastPageNo = tempLastPageNo;
		if( tempLastPageNo > this.lastPageNo) this.hasNext = true;
	}
	
	public String getPageStr()
	{
		String tempUri = "<a href='fabricMain.do?pageNo=:v1'>:v2</a>";
		StringBuffer buffer = new StringBuffer();
		if(this.hasPrevious) buffer.append(tempUri.replaceAll(":v1", ""+(this.firstPageNo-1)).replaceAll(":v2","<font size='3'> ◀ </font> &nbsp;&nbsp;"));
		for(int i = firstPageNo; i<=lastPageNo; i++)
		{
			if(pageNo == i)
			{
				buffer.append(tempUri.replaceAll(":v1", ""+i).replaceAll(":v2", "<font size='3' color='red'> &nbsp;  "+i + "&nbsp;</font>"));
				continue;
			}
			buffer.append(tempUri.replaceAll(":v1", ""+i).replaceAll(":v2", "<font size='3'> &nbsp;  "+i + "&nbsp;</font>"));
		}
		if(hasNext)buffer.append(tempUri.replaceAll(":v1", ""+(this.lastPageNo+1)).replaceAll(":v2", "&nbsp;<font size='3'> ▶ </font>"));
		return buffer.toString();
	}
	
	public static void main(String[] args) 
	{
		FabricPageUtil util = new FabricPageUtil(1, 101);
		System.out.println(util.getPageStr());
	}
}
